# 此接口为高级用户的查询接口

from Flask_run import db
from database.models.message import Message
from database.models.mri import Mri
from database.models.patient import Patient
from sqlalchemy import or_
def top_query(condition):
  result_list=[]
  if str(condition["find_state"]) == "1": # 开始查询
    # 为什么库里没有值就无法查询到
    # if not condition["name"]:
    #   # objs = Patient.query.filter(Patient.name.like('%{keyword}%'.format(keyword=condition["name"]))).all()
    #   condition["name"]="%"
        # Mri_items=Mri.query.join(Patient, Patient.id == Mri.patient_id).filter(Patient.name.like('%{keyword}%'.format(keyword=condition["name"]))).filter(Mri.check_date.like('%{keyword}%'.format(keyword=condition["check_date"]))).filter(Mri.subtype.like('%{keyword}%'.format(keyword=condition["p_sub"]))).filter(Mri.ai_invasion.like('%{keyword}%'.format(keyword=condition["p_invasive"]))).filter(Mri.ai_level.like('%{keyword}%'.format(keyword=condition["p_level"]))).all()

   
    Mri_items=Mri.query.join(Patient, Patient.id == Mri.patient_id)\
         .filter(Patient.name.like('%{keyword}%'.format(keyword=condition["name"])),
               or_(Mri.subtype==condition["p_sub"],condition["p_sub"]==''),
               or_(Mri.check_date.between(condition["check_date1"], condition["check_date2"]),condition["check_date1"]=='', condition["check_date2"]==''),
               or_(Mri.operation_date.between(condition["operation_date1"], condition["operation_date2"]),condition["operation_date1"]=='',condition["operation_date2"]==''),
               or_(Mri.patholog_invasion==condition["p_invasive"],condition["p_invasive"]==''),
               or_(Mri.pathology_level==condition["p_level"],condition["p_level"]=='')).all()
    
    
    print(Mri_items)
    for Mri_item in Mri_items:
      Patient_item = db.session.query(Patient.id,Patient.name,Patient.sex,Patient.age,Patient.pid,Patient.borntime,Patient.hospital).filter(Patient.id == Mri_item.patient_id).all()
      Patient_item=Patient_item[0]
      result={
"study_info":{
   "StudyInstanceUIDs":Mri_item.study_id,
    "id":Patient_item[4], 
    "name":Patient_item[1], 
    "f_num":Mri_item.fid,       
    "age":Patient_item[3], 
    "sex":'男' if Patient_item[2] == "M" else '女', 
    "bdate":Patient_item[5], 
    "cdate":Mri_item.check_date}, 
"image_info":{
    "device":Mri_item.device,   
    "modality":Mri_item.modality,  
    "slice":Mri_item.slice,
    "available":Mri_item.available}, 
"AI_info":{
    "invasive":Mri_item.ai_invasion,
    "level":Mri_item.ai_level}, 
"result_info":{"consist_inv":Mri_item.inv_consistence, "consist_lev":Mri_item.level_consistence},
"clinic_info":{"cheif": Mri_item.complaints, "remark": Mri_item.notes}, 
"surgery_info":{"date":Mri_item.operation_date, "region":Mri_item.Lesion_site,"zhuw":Mri_item.arachnoid,"xinp":Mri_item.simpson},
"pathology_info":{"p_invasive":Mri_item.patholog_invasion, "p_level":Mri_item.pathology_level, "vig":Mri_item.pathology_valueadded, "p_sub":Mri_item.subtype}}

      result_list.append(result)
    print(result_list)
    
    return result_list
      
    
  else: # 返回所有信息
    # Patient_table = db.session.query(Patient.id,Patient.name,Patient.sex,Patient.age,Patient.pid,Patient.borntime,Patient.hospital).all()
    # print(Patient_table)
    Mri_table = db.session.query(Mri.id,Mri.fid,Mri.patient_id,Mri.study_id,Mri.device,Mri.modality,Mri.slice,Mri.check_date,Mri.complaints,Mri.notes,Mri.ai_invasion,Mri.ai_level,Mri.operation_date,Mri.Lesion_site,Mri.arachnoid,Mri.simpson,Mri.adhesions,Mri.subtype,Mri.patholog_invasion,Mri.pathology_level,Mri.pathology_valueadded,Mri.inv_consistence,Mri.level_consistence,Mri.available).all()
    # print(Mri_table)
    for Mri_item in Mri_table:
      Patient_item = db.session.query(Patient.id,Patient.name,Patient.sex,Patient.age,Patient.pid,Patient.borntime,Patient.hospital).filter(Patient.id == Mri_item[2]).all()
      Patient_item=Patient_item[0]
      print(Patient_item)
      result={
"study_info":{
    "StudyInstanceUIDs":Mri_item[3],
    "id":Patient_item[4], 
    "name":Patient_item[1], 
    "f_num":Mri_item[1],       
    "age":Patient_item[3], 
    "sex": '男' if Patient_item[2] == "M" else '女', 
    "bdate":Patient_item[5], 
    "cdate":Mri_item[7]}, 
"image_info":{
    "device":Mri_item[4],   
    "modality":Mri_item[5],  
    "slice":Mri_item.slice,
    "available":Mri_item.available}, 
"AI_info":{
    "invasive":Mri_item[10],
    "level":Mri_item[11]}, 
"result_info":{"consist_inv":Mri_item.inv_consistence, "consist_lev":Mri_item.level_consistence}, 
"clinic_info":{"cheif": Mri_item[8], "remark": Mri_item[9]}, 
"surgery_info":{"date":Mri_item[12], "region":Mri_item[13],"zhuw":Mri_item[14],"xinp":Mri_item[15]},
"pathology_info":{"p_invasive":Mri_item[18], "p_level":Mri_item[19], "vig":Mri_item[20], "p_sub":Mri_item[17]}}

      result_list.append(result)
    print(result_list)
    return result_list
  
# if __name__ == '__main__':
#   condition={
# "find_state":1, #查询标志位：0为不查询，1为查询
# "name":"BAI_YUE_BIN", #患者姓名
# "check_date":"2019-05-13", #检查日期
# "p_sub":"脑膜皮型", #病理亚型
# "p_invasive":"是", #病理结果是否脑侵袭
# "p_level":"二级" #病理结果是否脑侵袭
# }
#   result=top_query(condition)